<?php

namespace app\models;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use yii\db\Query;
use yii;
class User extends ActiveRecord implements IdentityInterface
{

    public static function tableName()
    {
        return '{{user}}';
    }
    /**
     * Finds an identity by the given ID.
     * @param string|int $id the ID to be looked for
     * @return IdentityInterface the identity object that matches the given ID.
     * Null should be returned if such an identity cannot be found
     * or the identity is not in an active state (disabled, deleted, etc.)
     */
    public static function findIdentity($id)
    {
        return self::findOne($id);
    }

    /**
     * Finds an identity by the given token.
     * @param mixed $token the token to be looked for
     * @param mixed $type the type of the token. The value of this parameter depends on the implementation.
     * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
     * @return IdentityInterface the identity object that matches the given token.
     * Null should be returned if such an identity cannot be found
     * or the identity is not in an active state (disabled, deleted, etc.)
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {

    }

    /**
     * Returns an ID that can uniquely identify a user identity.
     * @return string|int an ID that uniquely identifies a user identity.
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Returns a key that can be used to check the validity of a given identity ID.
     *
     * The key should be unique for each individual user, and should be persistent
     * so that it can be used to check the validity of the user identity.
     *
     * The space of such keys should be big enough to defeat potential identity attacks.
     *
     * This is required if [[User::enableAutoLogin]] is enabled.
     * @return string a key that is used to check the validity of a given identity ID.
     * @see validateAuthKey()
     */
    public function getAuthKey()
    {

    }

    /**
     * Validates the given auth key.
     *
     * This is required if [[User::enableAutoLogin]] is enabled.
     * @param string $authKey the given auth key
     * @return bool whether the given auth key is valid.
     * @see getAuthKey()
     */
    public function validateAuthKey($authKey)
    {

    }

    /*新建用户*/
    public function createNew($name,$pass,$type,$mall_id=0)
    {
        $in_data=array(
            'name'=>$name,
            'pass'=>md5($pass),
            'type'=>$type,
            'mall_id'=>$mall_id,
            'create_time'=>time(),
        );
        #重名确认
        if($this->getOneByName($name))
        {
            return false;
        }
        if(Yii::$app->db->createCommand()->insert('user',$in_data)->execute())
        {
            return true;
        }
    }

    /*编辑用户*/
    public function editUser($id,$up_data)
    {
        $up_where=array(
            'id'=>$id,
        );
        if(isset($up_data['pass']))
        {
            $up_data['pass']=md5($up_data['pass']);
        }
        return Yii::$app->db->createCommand()->update('user',$up_data,$up_where)->execute();
    }

    /*根据店铺ID 获取用户信息*/
    public function getOneByMallId($mall_id)
    {
        $where=array(
            'mall_id'=>$mall_id
        );
        $info=(new Query())
            ->from('user')
            ->where($where)
            ->one();
        return $info;
    }

    /*根据店铺Name 获取用户信息*/
    public function getOneByName($name)
    {
        $where=array(
            'name'=>$name
        );
        $info=(new Query())
            ->from('user')
            ->where($where)
            ->one();
        return $info;
    }

    /*根据店铺ID 删除用户信息*/
    public function delByMallId($mall_id)
    {
        #删除用户
        $up_data=array(
            'is_del'=>'1',
        );
        $up_where=array(
            'mall_id'=>$mall_id,
        );
        return Yii::$app->db->createCommand()->update('user',$up_data,$up_where)->execute();
    }

}
